package com.businessportal.web.common;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.KeySpec;

/**
 * Created by Vadim on 9/18/14.
 */
public class CryptoHelper {
    private static CryptoHelper instance = new CryptoHelper();
    private Cipher cipher;
    private Key key;

    private CryptoHelper(){
       try{
           this.cipher = Cipher.getInstance("AES");
           byte[] raw = { (byte) 0xA5, (byte) 0x01, (byte) 0x7B, (byte) 0xE5,
                   (byte) 0x23, (byte) 0xCA, (byte) 0xD4, (byte) 0xD2,
                   (byte) 0xC6, (byte) 0x5F, (byte) 0x7D, (byte) 0x8B,
                   (byte) 0x0B, (byte) 0x9A, (byte) 0x3C, (byte) 0xF1 };
           this.key = new SecretKeySpec(raw, "AES");
       }catch(Exception e){

       }
    }

    public static CryptoHelper getInstance(){
        return instance;
    }

    public String encrypt(String data) throws Exception {

        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] utf8 = data.getBytes("UTF8");
        byte[] encryptedData = cipher.doFinal(utf8);
        return Base64.encode(encryptedData);

    }

    public String decrypt(String data) throws Exception {
        String result = "";
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodedData = Base64.decode(data);//this.b64Decoder.decodeBuffer(aData);
            byte[] utf8 = cipher.doFinal(decodedData);
            result = new String(utf8, "UTF8");
        }
        catch (InvalidKeyException oException) { 			oException.printStackTrace(); }
        catch (Base64DecodingException oException) { 		oException.printStackTrace(); }
        catch (IllegalBlockSizeException oException) { 		oException.printStackTrace(); }
        catch (BadPaddingException oException) { 			oException.printStackTrace(); }
        catch (UnsupportedEncodingException oException) { 	oException.printStackTrace(); }
        return result;
    }

    public String encodeToHex(String data) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] utf8 = data.getBytes("UTF8");
        byte[] encryptedData = cipher.doFinal(utf8);
        return Hex.encodeHexString(encryptedData);
    }

    public String decodeFromHex(String data) throws Exception {
        String result = "";
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodedData = Hex.decodeHex(data.toCharArray());//this.b64Decoder.decodeBuffer(aData);
            byte[] utf8 = cipher.doFinal(decodedData);
            result = new String(utf8, "UTF8");
        }
        catch (InvalidKeyException oException) { 			oException.printStackTrace(); }
        catch (IllegalBlockSizeException oException) { 		oException.printStackTrace(); }
        catch (BadPaddingException oException) { 			oException.printStackTrace(); }
        catch (UnsupportedEncodingException oException) { 	oException.printStackTrace(); }
        return result;
    }
}
